//
//  RootViewController.m
//  todo
//
//  Created by Brandon Trebitowski on 8/17/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "TaskListViewController.h"
#import "Gradekeeper0_2AppDelegate.h"
#import "Todo.h"
#import "TodoCell.h"
#import "TKCalendarViewController.h"

@implementation TaskListViewController
@synthesize todoView;

- (void)returnAction:(id)sender {
	[[self navigationController] popToRootViewControllerAnimated:YES];
	
} //returnAction

- (void)viewDidLoad {
	//self.toolbarItems = [NSArray arrayWithObjects: self.editButtonItem, nil];
	
	UIBarButtonItem *returnButton = [[UIBarButtonItem alloc]
									initWithTitle:@"Return" style:UIBarButtonItemStyleBordered target:self action:@selector(returnAction:)];
	

	
	self.toolbarItems = [NSArray arrayWithObjects:returnButton, nil];
	
	[returnButton release];
	self.navigationItem.leftBarButtonItem = self.editButtonItem;

	UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd 
															 target:self action:@selector(addTodo:)];
	self.navigationItem.rightBarButtonItem = btn;
}

- (void) addTodo:(id)sender {
	Gradekeeper0_2AppDelegate *appDelegate = (Gradekeeper0_2AppDelegate *)[[UIApplication sharedApplication] delegate];
	
	if(self.todoView == nil) {
		TodoViewController *viewController = [[TodoViewController alloc] 
											  initWithNibName:@"TodoViewController" bundle:[NSBundle mainBundle]];
		self.todoView = viewController;
		[viewController release];
	}
	
	Todo *todo = [appDelegate addTodo];
	
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/YY h:mm a";
	

	
	[self.navigationController setToolbarHidden:YES];
	[self.navigationController pushViewController:self.todoView animated:YES];

	self.todoView.todo = todo;
	self.todoView.title = todo.text;
	
	NSDate *today =  [[NSDate alloc] init];
	
	NSString *beginDate = [formatter stringFromDate:today];
	NSLog(@"the begin date is %@",beginDate);
	
	NSTimeInterval secondsPerDay = 24 * 60 * 60;
	today = [today addTimeInterval:secondsPerDay];
	NSString *endDate = [formatter stringFromDate:today];
	
	[self.todoView.todoText setText:todo.text];	
	[self.todoView.todoBeginDT setText:beginDate];
	[self.todoView.todoEndDT setText:endDate];
	[self.todoView.todoSummary setText:todo.summary];

	
	
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // Updates the appearance of the Edit|Done button as necessary.
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:YES];
    // Disable the add button while editing.
    if (editing) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	Gradekeeper0_2AppDelegate *appDelegate = (Gradekeeper0_2AppDelegate *)[[UIApplication sharedApplication] delegate];
    return appDelegate.todos.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	TodoCell *cell = (TodoCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[TodoCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	Gradekeeper0_2AppDelegate *appDelegate = (Gradekeeper0_2AppDelegate *)[[UIApplication sharedApplication] delegate];
	Todo *td = [appDelegate.todos objectAtIndex:indexPath.row];
	
	[cell setTodo:td];
	
	// Set up the cell
	return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	 Gradekeeper0_2AppDelegate *appDelegate = (Gradekeeper0_2AppDelegate *)[[UIApplication sharedApplication] delegate];
	 Todo *todo = (Todo *)[appDelegate.todos objectAtIndex:indexPath.row];
	 
	 if(self.todoView == nil) {
		 TodoViewController *viewController = [[TodoViewController alloc] 
											   initWithNibName:@"TodoViewController" bundle:[NSBundle mainBundle]];
		 self.todoView = viewController;
		 [viewController release];
	 }
	
	 
	 [self.navigationController pushViewController:self.todoView animated:YES];
	 self.todoView.todo = todo;
	 self.todoView.title = todo.text;
	 [self.todoView.todoText setText:todo.text];
	 
	 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	 formatter.dateFormat = @"MM/dd/YY h:mm a";
	//NSString *beginDate = [formatter stringFromDate:today];
	 [self.todoView.todoBeginDT setText:[formatter stringFromDate:todo.beginDate]];
	 [self.todoView.todoEndDT setText:[formatter stringFromDate:todo.endDate]];
	 [self.todoView.todoSummary setText:todo.summary];
	 
	 
	 //to be deleted
	 /*
	 NSInteger priority = todo.priority - 1;
	 if(priority > 2 || priority < 0) {
		 priority = 1;
	 }
	 priority = 2 - priority;
	 
	 [self.todoView.todoPriority setSelectedSegmentIndex:priority];
	 
	 if(todo.status == 1) {
		 [self.todoView.todoButton setTitle:@"Mark As In Progress" forState:UIControlStateNormal];
		 [self.todoView.todoButton setTitle:@"Mark As In Progress" forState:UIControlStateHighlighted];
		 [self.todoView.todoStatus setText:@"Complete"];
	 } else {
		 [self.todoView.todoButton setTitle:@"Mark As Complete" forState:UIControlStateNormal];
		 [self.todoView.todoButton setTitle:@"Mark As Complete" forState:UIControlStateHighlighted];
		  [self.todoView.todoStatus setText:@"In Progress"];
	 }
	  */
}



// Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:
	(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
	Gradekeeper0_2AppDelegate *appDelegate = (Gradekeeper0_2AppDelegate *)[[UIApplication sharedApplication] delegate];
	Todo *todo = (Todo *)[appDelegate.todos objectAtIndex:indexPath.row];
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		[appDelegate removeTodo:todo];
		// Delete the row from the data source
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
	}	
}



/*
 Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the specified item to be editable.
	return YES;
}
*/


/*
 Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
 Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the item to be re-orderable.
	return YES;
}
 */ 

- (void)viewWillAppear:(BOOL)animated {
	[self.tableView reloadData];
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}


@end

